package com.feeyo.groundservice.composewidget.util

import androidx.annotation.DimenRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit

/**
 *作者：QLH on 2023-08-01
 *描述：compose的工具类：尺寸转换
 */


var SCALE_FONT_SIZE = 1f

/**
 * 将dimen值转换为 Dp
 * @return Dp 值
 */
@Stable
@Composable
fun @receiver:DimenRes Int.dimenToDp(): Dp = dimensionResource(this)

/**
 * 将像素值转换为 Dp
 * @return Dp 值
 */
@Stable
@Composable
fun Int.pxToDp(): Dp = with(LocalDensity.current) {
    this@pxToDp.toDp()
}

/**
 * 将像素值转换为 Dp
 * @return Dp 值
 */
@Stable
@Composable
fun Float.pxToDp(): Dp = with(LocalDensity.current) {
    this@pxToDp.toDp()
}

/**
 * 将 Dp 值转换为像素值
 * @return 像素值
 */
@Stable
@Composable
fun Dp.dpToPx(): Float = with(LocalDensity.current) {
    this@dpToPx.toPx()
}

/**
 * 将像素值转换为 sp
 * @return sp 值
 */
@Stable
@Composable
fun Int.pxToSp(): TextUnit = with(LocalDensity.current) {
    this@pxToSp.toSp() * SCALE_FONT_SIZE
}

/**
 * 将像素值转换为 sp
 * @return sp 值
 */
@Stable
@Composable
fun Float.pxToSp(): TextUnit = with(LocalDensity.current) {
    this@pxToSp.toSp() * SCALE_FONT_SIZE
}

/**
 * 将dimension转换为 sp
 * @return sp 值
 */
@Stable
@Composable
fun @receiver:DimenRes Int.dimenToSp(): TextUnit = with(LocalDensity.current) {
    this@dimenToSp.dimenToDp().toSp() * SCALE_FONT_SIZE
}